const config = require('../../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    buildingArr: [],
    buildingIndex: 0,
    buildingIdArr: 0,
    buildingOrder: '请选择楼号',
    changeBuildingColor: true,
    addName: '',
    addTel: '',
    addressInput: '',
    switchChecked: false,
    buildingAddress: '',


    telVal: '',
    nameVal: '',
    addressVal: '',
    isNameEdit: false,
    isTelEdit: false,
    isAddressEdit: false,
    isSwitchChecked: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    wx.request({
      url: config.buildingUrl,
      method: 'GET',
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      success: function(res) {
        // console.log(res)
        var buildingArr = [];
        var buildingIdArr = [];
        var buildingAll = [];
        res.data.forEach((val, index) => {
          buildingArr.push(res.data[index].address_name);
          buildingIdArr.push(res.data[index].id);
          buildingAll.push(res.data[index])
        })
        that.setData({
          buildingArr: buildingArr,
          buildingIdArr: buildingIdArr,
          buildingAll: buildingAll
        })
      }
    });
    wx.request({
      url: config.oneAddress + `${options.id}`,
      method: 'GET',
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      success: (res) => {
        // console.log(res)
        that.setData({
          addInfo: res.data,
          nameVal: res.data.name,
          telVal: res.data.tel,
          addressVal: res.data.send_address,
          buildingInfo: res.data.building,
          changeBuildingColor: false,
          switchChecked: res.data.status == '1' ? true : false,
          id: res.data.id,
          pBuildingId: res.data.building_id
        })
        that.setData({
          buildingOrder: that.data.buildingInfo
        })
      },
    })
  },

  delete: function(e) {
    // console.log(e)
    let that = this;
    let addressId = e.currentTarget.dataset.id;
    wx.showModal({
      title: '删除地址',
      content: '您确定要删除地址吗？',
      success: (res) => {
        if (res.confirm) {
          wx.request({
            url: config.delAddress,
            method: 'POST',
            data: {
              id: addressId
            },
            header: {
              'Authorization': 'Bearer ' + wx.getStorageSync('token'),
              APPID: 28
            },
            success: (res) => {
              wx.showToast({
                title: '删除成功',
                icon: 'none',
                duration: 1500
              })
              setTimeout(() => {
                wx.navigateBack({
                  url: '/pages/address/address',
                })
              }, 1500)
            }
          })
        }
      }
    })
  },
  bindBuildingChange: function(e) {
    // console.log(e)
    let index = e.detail.value;
    this.setData({
      buildingOrder: this.data.buildingArr[index],
      changeBuildingColor: false,
      buildingAddress: this.data.buildingArr[index],
      buildingId: this.data.buildingIdArr[index]
    })
    // console.log(this.data.buildingAddress)
  },

  addName: function(e) {
    // console.log(e)
    this.setData({
      addName: e.detail.value,
      isNameEdit: true
    })
  },
  addTel: function(e) {
    // console.log(e)
    this.setData({
      addTel: e.detail.value,
      isTelEdit: true
    })
  },

  addressInput: function(e) {
    // console.log(e)
    this.setData({
      addressInput: e.detail.value,
      isAddressEdit: true
    })
  },

  switchChange: function(e) {
    // console.log(e);
    this.setData({
      switchChecked: e.detail.value,
    })
  },

  saveAddress: function() {
    let addName = this.data.addName;
    let addTel = this.data.addTel;
    let buildingId = this.data.buildingId;
    let buildingAddress = this.data.buildingAddress;
    let addressInput = this.data.addressInput;
    let switchChecked = this.data.switchChecked;
    let nameVal = this.data.nameVal;
    let telVal = this.data.telVal;
    let addressVal = this.data.addressVal;
    let pName;
    let pTel;
    let poBuildingId;
    let pDetailAdd;
    let id = this.data.id
    let isStatus = false;
    if (this.data.isNameEdit) { //如果改过
      if (addName == '') {
        wx.showToast({
          title: '请填写收件人',
          icon: 'none'
        })
        return
      } else {
        pName = addName
      }
    } else {
      pName = nameVal
    }

    if (this.data.isTelEdit) { //如果改过
      if (addTel == '') {
        wx.showToast({
          title: '请填写联系电话',
          icon: 'none'
        })
        return 
      } else {
        pTel = addTel
      }
    } else {
      pTel = telVal
    }

    if (this.data.buildingInfo == this.data.buildingOrder) {
      poBuildingId = this.data.pBuildingId;
      // console.log(poBuildingId)
    } else {
      poBuildingId = buildingId
      // console.log(poBuildingId)
    }

    if (this.data.isAddressEdit) { //如果改过
      if (addressInput == '') {
        wx.showToast({
          title: '请填写详细地址',
          icon: 'none'
        })
        return
      } else {
        pDetailAdd = addressInput
      }
    } else {
      pDetailAdd = addressVal
    }
    wx.request({
      url: config.editAddress,
      method: 'POST',
      data: {
        name: pName,
        tel: pTel,
        building_id: poBuildingId,
        send_address: pDetailAdd,
        status: switchChecked ? 1 : 0,
        id: id
      },
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      success: (res) => {
        // console.log(res)
        if (res.data == 'success') {
          wx.showToast({
            title: '保存成功',
            icon: 'success',
            duration: 1500,
            success: () => {
              setTimeout(() => {
                wx.navigateBack({
                  url: '/pages/address/address',
                })
              }, 1500)
            }
          })
        }
      }
    })
  },
  
})